/*文章页js*/
//填充最新文章
function putInNewArticle(data) {
    var newArticle = $('#newArticle');
    newArticle.empty();
    $.each(data['result'],function (index,obj) {
        var li = $('<li><a href="'+ obj['articleUrl']+'">'+ obj['articleTitle'] +'</a></li> ');
        newArticle.append(li);
    });
}
//填充最热文章
function putInhotArticleAjax(data) {
    var hotArticle = $('#hotArticle');
    hotArticle.empty();
    $.each(data['result'],function (index,obj) {
        var li = $('<li><a href="'+ obj['articleUrl']+'">'+ obj['articleTitle'] +'</a></li> ');
        hotArticle.append(li);
    });
}
//填充标签云
function putInTagsCloud(data){
    var tagCloud = $('#tagCloud');
    tagCloud.empty();
    $.each(data['result'], function (index, obj) {
        tagCloud.append($('<a href="/findArticle/toTag/' + obj['tagName'] + '" >' + obj['tagName'] + '</a>'));
    });
}
function newArticleAjax(currentPage) {
    //最新发布的文章
    $.ajax({
        type: 'GET',
        url: '/main/newArticle',
        dataType: 'json',
        data: {
            rows:"5",
            pageNum:currentPage
        },
        success: function (data) {
            putInNewArticle(data);
        },
        error: function () {
            alert("获得最新文章信息失败！");
        }
    });
}
function hotArticleAjax(currentPage) {
    //最热文章
    $.ajax({
        type: 'GET',
        url: '/main/hotArticle',
        dataType: 'json',
        data: {
            rows:"5",
            pageNum:currentPage
        },
        success: function (data) {
            putInhotArticleAjax(data);
        },
        error: function () {
            alert("获得最热文章信息失败！");
        }
    });
}
//标签云
$.ajax({
    type: 'GET',
    url: '/main/findTagsCloud',
    dataType: 'json',
    data: {
    },
    success: function (data) {
        if(data['result'].length == 0){
            var tagCloud = $('#tagCloud');
            tagCloud.empty();
            var widgetTagCloud = $('<a href="/">暂无标签</a>');
            tagCloud.append(widgetTagCloud);
            $('#right').append(tagCloud);
        } else {
            putInTagsCloud(data);
        }

    },
    error: function () {
        alert("获得标签云失败！");
    }
});
newArticleAjax(1);
hotArticleAjax(1);
